<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0%;
            padding: 0%;
            box-sizing: border-box;
        }
        .line{
            width: 1px;
            height:2000px;
            position: absolute;
            background-color: red;
            left: 800px;
        }
        .box1{
            width: 200px;
            height: 200px;
            background-color: orange;
            position: absolute;
            left: 0;
        }
    </style> 
</head>
<body>
    <button class="btn1">向左</button>
    <button class="btn2">向右</button>
    <div class="line"></div>
    <div class="box1"></div>
    <script src="../js/tools.js"></script>
    <script>
        let btn1=$(".btn1")
        let btn2=$(".btn2")
        let box1=$(".box1")
        timer=null
        btn1.onclick=function(){
            clearInterval(timer)
            timer=setInterval(function(){
            let start=box1.offsetLeft
            let step=10
            let end=start+step

            if(end>=800){
                end=800;
                clearInterval(timer)
            }
            box1.style.left=end+"px"
            },30)
        }
        btn2.onclick=function(){
            clearInterval(timer)
            timer=setInterval(function(){
            let start=box1.offsetLeft
            let step=-10
            let end=start+step

            if(end<0){
                end=0;
                clearInterval(timer)
            }
            box1.style.left=end+"px"
            },30)
        }
    </script>
</body>
</html>